<?php
namespace app\api\controller;

use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use think\facade\Cache;

class AcceptLss
{
    private $redis;

    public function __construct()
    {
        $this->redis = Cache::store('redis')->handler();
    }

    public function index()
    {
        $params = Request::param();
        //记录日志
        $time   = getUtcTime(false);
        Log::write("-------------LSS({$time})-----------------------start\n");
        Log::write($params);
        Log::write("\n");

        if(!is_array($params)){
            $params = json_decode($params , true);
        }
        try {
            $list = $params[0];
            $ctime= getUtcTime();
            if(isset($list['data']['cardGuid'])) {
                $eventType = $list['eventType'];
                $eventTime = $list['eventTime'] ?? '';
                $lss_time = '';
                if (!empty($eventTime)) {
                    $eventTime = str_replace('T', ' ', $eventTime);
                    $eventTime = substr($eventTime, 0, 19);
                    $lss_time = date('Y-m-d H:i:s', strtotime($eventTime . ' -5 hour'));
                }
                $tran_guid = $list['id'];
                $api_element_json = json_encode($list);

                $list_data = $list['data'];
                $card_token = $list_data['cardGuid'];
                $accept_amount = $list_data['amount'] ?? '';
                $availableBalance = $list_data['availableBalance'] ?? '';
                $accept_app_code = $list_data['authCode'] ?? '';
                $shopping_msg = $list_data['cardAcceptorName'] ?? '';

                $card_list = Db::name('cards')->where(['card_token' => $card_token])->find();
                //记录非vvacard的卡CardManage->非本银3
                if (empty($card_list)) {
                    Db::name('cd_api_error')->save([
                        'user_id' => 0,
                        'type' => 'no_find_card(lss)',
                        'tran_token' => $card_token,
                        'desc' => '',
                        'ctime' => $ctime,
                        'eventType' => $eventType,
                        'amount' => $accept_amount,
                        'availableBalance' => $availableBalance,
                    ]);

                    Log::write("Error: Card not find(LSS) \n");
                }else{
                    $user_id = $card_list['user_id'];
                    $card_num = $card_list['card_num'];

                    //接收webhook log日志
                    Db::name('api_accept_lss')->insertGetId([
                        'user_id' => $user_id,
                        'card_num' => $card_num,
                        'amount' => $accept_amount,
                        'availableBalance' => $availableBalance,
                        'app_code' => $accept_app_code,
                        'tran_guid' => $tran_guid,
                        'eventType' => $eventType,
                        'eventTime' => $eventTime,
                        'ctime' => $lss_time,
                        'shopping' => $shopping_msg,
                        'json_data' => $api_element_json,
                        'is_action' =>0
                    ]);
                }
            }

            if(isset($list['eventType']) == 'sale.cash.balance.funding'){
                $type =   $list['data']['type'] ?? '';
                if(!empty($type)){
                    $date_real = $list['data']['dateTime'];
                    $date      = substr($date_real , 0 , 19);
                    $date      = str_replace('T' , ' ' , $date);
                    $date      = date('Y-m-d H:i:s' , strtotime($date) + 8*60*60);

                    $insert_data =[
                        "clientName" => $list['data']['clientName'],
                        "mid" => $list['data']['mid'],
                        "date" => $date, //webhooks推送时间 + 8小时
                        "date_real" => $date_real, //webhooks推送时间
                        "type" => $list['data']['type'],
                        "amount" => $list['data']['amount'],
                        "availableBalance" => $list['data']['availableBalance'],
                    ];
                    $a = Db::name('lss_fund_user')->save($insert_data);
                }
            }
        }catch (\Exception $e){
            Log::write($e->getMessage() . '----' . $e->getLine());
        }
        Log::write("-------------LSS({$time})-----------------------end\n");

        http_response_code(200);
        echo "success";
    }

}
